//
//  RecordListController.m
//  Unzip
//
//  Created by zhibao on 2024/8/4.
//

#import "RecordListController.h"
#import "RecordingController.h"
@interface RecordListController ()<UITableViewDelegate,UITableViewDataSource,DataChangeDelegate,UIDocumentInteractionControllerDelegate>{
    
}
@property(nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)UIDocumentInteractionController * document;
//路径前缀
@property(nonatomic,strong)NSString *docum;
@property(nonatomic,strong)NSString *yulanPath;
@property (nonatomic,strong) MYTableView *showTableView;
@end

@implementation RecordListController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = LocalizedString(@"录音列表");
    self.docum = @"Tools/Record";
    self.dataArray = [[NSMutableArray alloc] init];
    [self initTableview];
    
}
-(void)getData{
    [self.dataArray removeAllObjects];
    [self.dataArray addObjectsFromArray:[Helper contentsOfDirectoryAtPath:self.docum error:nil]];
    DLog(@"dataArray====%@",self.dataArray);
    [self.showTableView reloadData];
}
-(void)initTableview{
    self.showTableView = [[MYTableView alloc]initWithFrame:CGRectMake(0,NAVIH, MY_Screen_W, MY_Screen_H - NAVIH) style:UITableViewStylePlain];
    self.showTableView.delegate = self;
    self.showTableView.dataSource = self;
    self.showTableView.dataChangeDelegate = self;
    
    self.showTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.showTableView];
    [self.showTableView setNoDataType:TableViewNoDataType_Data];
    [self.showTableView.mj_footer endRefreshingWithNoMoreData];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //[btn setImage:[UIImage systemImageNamed:@"waveform"] forState:UIControlStateNormal];
    [btn setTintColor:UIColor.whiteColor];
    btn.backgroundColor = MyColorAlpha(@"#ff8070", 0.8);
    [btn setTitle:@"+" forState:UIControlStateNormal];
    [btn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
//    [btn setButtonImageAlignmentStyle:WMButtonAlignmentStyleTop imageAndLabelSpace:pixw(3)];
    btn.layer.cornerRadius = pixw(35);
    btn.titleLabel.font = MY_SCALE_BoldFont(30);
    [btn addTarget:self action:@selector(toRecordIngClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(pixw(-20));
        make.width.height.mas_equalTo(pixw(70));
        make.bottom.mas_equalTo(-SafeBottomMargin - pixw(80));
    }];
}

-(void)toRecordIngClick{
    RecordingController *controller = [RecordingController new];
    controller.docum = self.docum;
    [self.navigationController pushViewController:controller animated:YES];
}


- (void)refreshDataWithTabelViewTag:(NSInteger)tableViewTag{
    
}
- (void)loadMoreDataWithTabelViewTag:(NSInteger)tableViewTag{
    
}


#pragma mark - tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"HomeCell";
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell=[[HomeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    [cell updataWithName:self.dataArray[indexPath.row] docum:self.docum flag:YES];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *name = self.dataArray[indexPath.row];
    _yulanPath = [NSString stringWithFormat:@"%@/%@/%@",[Helper getPath],self.docum,name];
    
    self.document = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:_yulanPath]];
    self.document.delegate = self;
    BOOL canOpen = [self.document presentPreviewAnimated:YES];
    
    if (!canOpen) {
        DLog(@"预览失败");
    }
}
-(BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(nullable SEL)action{
    // 响应方法
    NSLog(@"12 %s", __func__);
    return YES;
}
-(BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(nullable SEL)action{
    //
    NSLog(@"13 %s", __func__);
    return YES;
}
-(void)documentInteractionControllerWillPresentOptionsMenu:(UIDocumentInteractionController *)controller{
    // 页面显示后响应
    NSLog(@"9 %s", __func__);
}
-(void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller{
    // 取消时响应
    NSLog(@"10 %s", __func__);
}
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    NSLog(@"1 %s", __func__);
    return self;
}
-(UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    NSLog(@"2 %s", __func__);
    return self.view;
}
-(CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    NSLog(@"3 %s", __func__);
    return self.view.frame;
}
// 文件分享面板退出时调用
-(void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller{
    NSLog(@"4 %s", __func__);
    NSLog(@"dismiss");
}
// 文件分享面板弹出的时候调用
-(void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller{
    NSLog(@"5 %s", __func__);
    NSLog(@"WillPresentOpenInMenu");
}
// 当选择一个文件分享App的时候调用
-(void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application{
    NSLog(@"6 %s", __func__);
    NSLog(@"begin send : %@", application);
}
// Preview presented/dismissed on document.  Use to set up any HI underneath.
-(void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller{
    NSLog(@"7 %s", __func__);
}
-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller{
    // 完成时响应
    NSLog(@"8 %s", __func__);
}
-(void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(nullable NSString *)application{
    NSLog(@"11 %s", __func__);
}

@end
